﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NewAI.Game;
using NewAI.Tools;
namespace NewAI.FSM.TankFSM
{
    public class AILogicState : State<TankAI>
    {
        private static Random rand = new Random();
        private RoundData round;
        private TankAI entity;
        public AILogicState(RoundData round)
        {
            this.round = round;
        }

        public override void Enter(TankAI entity)
        {
            entity.RoundID = round.RoundID;
            this.entity = entity;
            entity.ThreatDegree = 1f;
            entity.CalculateTime = -1f;
            entity.ProfitDegree = 1f;
            entity.IfHasAssistanceMission = false;

            //检查是否已经死了
            if (entity.IsSurvival == false)
                return;
            entity.IsSurvival = false;
            foreach (var item in round.Players)
            {
                if (item.TeamID == GV.OurTeam.TeamID && item.Id == entity.TankID)
                {
                    entity.IsSurvival = true;
                    entity.Position = new System.Drawing.Point(item.Position.X, item.Position.Y);
                }
            }

            if (entity.IsSurvival == false)
            {
                GV.tankCount--;
                return;
            }

            RoundAnalyzer threatDegreeAnalyzer = new RoundAnalyzer();
            threatDegreeAnalyzer.GetThreatDegreeAndProfitDegree(ref entity, ref round);

            //AnalyseMethod();
            entity.NoticeTankTOFight();


            // analyzerAndSendActionThread = new Thread(new ThreadStart(AnalyzerAndSendAction));
            //analyzerAndSendActionThread.Start();
        }

        private void AnalyseMethod()
        {
            float threat = entity.ThreatDegree;
            float profit = entity.ProfitDegree;
            float total = threat + profit;
            float threatPercentage;

            threatPercentage = threat / total * 100;
            if (threatPercentage > 100)
                threatPercentage = 100;
            if (threatPercentage < 0)
                threatPercentage = 0;

            #region 特殊情况
            if (threat >= 70f)//威胁度过高
            {
                PubliceMessage.Debug($"TankID = { entity.TankID}  决策：威胁度过高→逃");
                entity.NoticeTankWithdraw();
                return;
            }

            if (profit >= 90f)//能够一回合击毙敌人
            {
                PubliceMessage.Debug($"TankID = { entity.TankID}  决策：能够一回合击毙敌人→进攻");
                entity.NoticeTankTOFight();
                return;
            }

            if (profit >= 70f)//收益丰厚
            {
                PubliceMessage.Debug($"TankID = { entity.TankID}  决策：收益丰厚→采矿");
                entity.NoticeTankGetCoin();
                return;
            }
            #endregion

            #region 一般情况
            if (threatPercentage >= 55f)//危险较高
            {
                PubliceMessage.Debug($"TankID = { entity.TankID}  决策：危险较高→请求援助");
                entity.NoticeTankAskForAssistance();
                return;
            }

            if (threatPercentage >= 50f)//危险一般
            {
                PubliceMessage.Debug($"TankID = { entity.TankID}  决策：危险一般→进攻");
                entity.NoticeTankTOFight();
                return;
            }

            if (threatPercentage <= 45f)//收益较高
            {
                PubliceMessage.Debug($"TankID = { entity.TankID}  决策：收益较高→采矿");
                entity.NoticeTankGetCoin();
                return;
            }
            PubliceMessage.Debug($"TankID = { entity.TankID}  决策：默认→进攻");

            entity.NoticeTankTOFight();
            #endregion
        }





        private void AnalyzerAndSendAction()
        {
            RoundAnalyzer threatDegreeAnalyzer = new RoundAnalyzer();
            threatDegreeAnalyzer.GetThreatDegreeAndProfitDegree(ref entity, ref round);
            //entity.NoticeTankTOFight();
            //TestSendActionObj();
        }

        public override void Exit(TankAI entity)
        {
            //if (analyzerAndSendActionThread != null && analyzerAndSendActionThread.IsAlive)
            //    analyzerAndSendActionThread.Abort();
            entity = null;
        }
        private void TestSendActionObj()
        {
            Direction movDir;
            int random = rand.Next(1, 5);
            switch (random)
            {
                case 1: movDir = Direction.Up; break;
                case 2: movDir = Direction.Down; break;
                case 3: movDir = Direction.Left; break;
                case 4: movDir = Direction.Right; break;
                default:
                    movDir = Direction.Up;
                    break;
            }

            Direction firDir;
            int firRandom = rand.Next(1, 5);
            switch (firRandom)
            {
                case 1: firDir = Direction.Up; break;
                case 2: firDir = Direction.Down; break;
                case 3: firDir = Direction.Left; break;
                case 4: firDir = Direction.Right; break;
                default:
                    firDir = Direction.Up;
                    break;
            }

            ActionObj obj = new ActionObj(GV.OurTeam.TeamID, entity.TankID, movDir, firDir, BulletType.Normal);
            entity.FinishTankActionMessage.Call(null, new FSMEventEventArg(obj));
        }
    }
}
